var fs = require('fs');
var request = require('request');
var superagent = require('superagent');
var superagentCharset = require('superagent-charset');
superagentCharset(superagent);

module.exports = (function () {
    let self;
    return {
        init: function () {
            self=this;
            return this;
        },
        progress: function (title,p='') {
            console.info('\n====Progress:'.info);
            console.log(title,p);
        },
        mkDir: dirPath => new Promise((resolve, reject) => {
            fs.exists(dirPath, exists => {
                if (exists)return resolve('exists');
                fs.mkdir(dirPath, (err) => {
                    if (err)return reject(err);
                    resolve('created');
                });
            });
        }),
        getPage: (uri, query = {}) => new Promise((resolve, reject) => {
            request({
                url:uri,
                method: 'get',
                qs: query,
                headers: {
                    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36',
                    Referer: 'http://book.jd.com/'
                }
            }, (err, res, body) => {
                if (err)return reject(err);
                resolve(body);
            });
        }),
        getGBKPage:(uri)=>new Promise((resolve,reject)=>{
            superagent.get(uri)
                .charset('gbk')
                .set('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36')
                .end((err, res)=> {
                    if(err)return reject(err);
                    resolve(res.text);
                });
        }),
        access:(filePath)=>new Promise((resolve)=>{
            fs.access(filePath,err=>{
                resolve(err?false:true);
            });
        }),
        readFile: (filePath,opts={}) => new Promise((resolve, reject) => {
            fs.readFile(filePath,opts, (err, ret) => {
                if (err)return reject(err);
                resolve(ret);
            });
        }),
        writeFile: (filePath, data,opts={}) => new Promise((resolve, reject) => {
            fs.writeFile(filePath, data,opts, (err, ret) => {
                if (err)return reject(err);
                resolve(ret);
            });
        }),
        error:function (title,err={}) {
            console.error('\nERROR:'.error);
            console.log(title);
            console.log(err);
            console.log('\n');
            process.exit(1);
        },
        warn:function (title,warn={}) {
            console.warn('\nWARN:'.warn);
            console.log(title);
            console.log(warn);
            console.log('\n');
        },
        flatten: function (list) {
            let ret=[];
            for(let item of list){
                if(Array.isArray(item))ret=ret.concat(self.flatten(item));
                else ret.push(item);
            }
            return ret;
        }
    }
})().init();